package com.youxue.teachin.common.utils;

import java.lang.ref.SoftReference;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import android.app.Activity;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-07 16:05
 * @version: 9.1.0
 */
public class ActivityCacheHelper {
    public static final Set<SoftReference<Activity>> mActivity = new HashSet<>(8);

    public static void registActivity(Activity activity) {
        mActivity.add(new SoftReference<Activity>(activity));
    }

    public static void unRegistActivity(Activity activity) {
        Iterator<SoftReference<Activity>> iterator = mActivity.iterator();
        while (iterator.hasNext()) {
            SoftReference<Activity> softReference = iterator.next();
            Activity activity1 = softReference.get();
            if (activity1 == activity) {
                iterator.remove();
                break;
            }
        }
    }

    public static void clear() {
        mActivity.clear();
    }

    public static void finish() {
        Iterator<SoftReference<Activity>> iterator = mActivity.iterator();
        while (iterator.hasNext()) {
            SoftReference<Activity> softReference = iterator.next();
            Activity activity1 = softReference.get();
            if (activity1 != null) {
                activity1.finish();
            }
        }
        clear();
    }
}
